package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.PatientDto;
import primetest.impl.dao.PatientDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.PatientService;

public class PatientService implements  Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -8060430686329121620L;
	private Transaction transaction;
	private PatientDao patientDao;

	@Inject
	public PatientService(Transaction transaction,PatientDao officeDao){
		this.transaction = transaction;
		this.patientDao = officeDao;
	}

	public List<PatientDto> getPatientList() throws IOException {
		try{
			transaction.begin();
			List<PatientDto> officeList = patientDao.getPatientList(transaction);
			//transaction.commit();
			return officeList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public PatientDto getPatient(int patientSeq) throws IOException {
		try{
			transaction.begin();
			PatientDto patientDto = patientDao.getPatient(transaction, patientSeq);
			//transaction.commit();
			return patientDto;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void addPatient(PatientDto officeDto) throws IOException {
		try{
			transaction.begin();
			patientDao.addPatient(transaction, officeDto);
			patientDao.getPatientList(transaction);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updatePatient(PatientDto officeDto) throws IOException {
		try{
			transaction.begin();
			patientDao.updatePatient(transaction, officeDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deletePatient(int officeSeq) throws IOException {
		try{
			transaction.begin();
			patientDao.deletePatient(transaction, officeSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

}
